package com.iwhalecloud.bss.kite.cucc.web.controller.order;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.order.IOrderService;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.OrderInfoListReq;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.OrderListInfo;
import com.iwhalecloud.bss.kite.cucc.client.dto.pay.PayOrderInfoReqDTO;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.cache.DcSystemParamCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.manager.transaction.KiteTransactional;
import com.ztesoft.bss.common.util.ContextUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
@Api(tags = { "/order - 订单信息" })
public class OrderController {

	@Autowired
	private IOrderService orderService;

	@KiteTransactional
    @ApiOperation(value = "获取订单详情", notes = "获取订单详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "orderId", value = "订单标识", required = true)
    })
    @GetMapping("/detail")
	public DataResult<Map<String, Object>> getOrderDetail(@RequestParam String orderId) {
		return ErrorConsts.SUCCESS.getResult(orderService.getOrderDetail(null, orderId));
	}

    @KiteTransactional
    @ApiOperation(value = "集客变更单信息获取", notes = "集客变更单信息获取")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "productId", value = "用户产品id", required = true),
        @ApiImplicitParam(name = "custId", value = "客户id", required = true),
        @ApiImplicitParam(name = "serialNumbe", value = "业务号码", required = true)
    })
    @GetMapping("/changeOriginOrder")
    public DataResult<Map<String, Object>> getChangeOriginOrder(@RequestParam String productId,@RequestParam String custId,@RequestParam String serialNumbe) {
        String provinceId = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        String lanId = ContextUtil.getLoginInfo().getUserInfo().getPostLanId();
        String staffCode = (String) ContextUtil.getLoginInfo().getUserInfo().getExtParams().get("account");
        return ErrorConsts.SUCCESS.getResult(orderService.getChangeOriginOrder(productId,custId, serialNumbe, provinceId, lanId, staffCode, ""));
    }

    /**
     * 订单列表查询
     * @author zhang.song
     * @date 2021-04-16 21:19
     * @param orderInfoListReq
     * @return com.iwhalecloud.bss.kite.client.dto.DataResult<com.iwhalecloud.bss.kite.cucc.client.dto.order.OrderListInfo>
     */
    @PostMapping("/queryOrderList")
    public DataResult<OrderListInfo> queryOrderList(@RequestBody OrderInfoListReq orderInfoListReq) {
        return ErrorConsts.SUCCESS.getResult(orderService.queryOrderList(orderInfoListReq));
    }

    @KiteTransactional
    @ApiOperation(value = "订单缴费列表查询", notes = "订单缴费列表查询")
    @PostMapping("/queryPayOrderList")
    public DataResult<OrderListInfo> queryPayOrderList(@RequestBody PayOrderInfoReqDTO payOrderInfoReqDTO) {
        return ErrorConsts.SUCCESS.getResult(orderService.queryPayOrderList(payOrderInfoReqDTO));
    }

    @KiteTransactional
    @ApiOperation(value = "根据原单查询落地方/收费方产品OfferId、goodsSku", notes = "根据原单查询落地方/收费方产品OfferId、goodsSku")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "originalOrderId", value = "原单编码", required = true),
        @ApiImplicitParam(name = "origin", value = "类型（1：落地方；2：收费方）", required = true)
    })
    @GetMapping("/getOfferIdByOriginalOrderId")
    public DataResult<Map<String, String>> getOfferIdByOriginalOrderId(@RequestParam String originalOrderId,
            @RequestParam(value="origin", required=false, defaultValue = "1") String origin) {
        return ErrorConsts.SUCCESS.getResult(orderService.getOfferIdByOriginalOrderId(originalOrderId, origin));
    }

    @KiteTransactional
    @ApiOperation(value = "获取订单详情：起租页面详情链接", notes = "获取订单详情：起租页面详情链接")
    @GetMapping("/getQzOrderDetailUrl")
    public DataResult<String> getQzOrderDetailUrl() {
        //起租页面详情调订单中心的地址,后台返回订单中心的ip,参数前端传
        //获取当前环境
        String type = DcSystemParamCache.getDcParamVal(KeyConsts.DC_SYSTEM_PARAM_ENV_TYPE);
        List<DcPublic> dcPublic = DcPublicCache.getByPkey("202203091808",type);
        //获取订单中心的地址
        String qzOrderDetailUrl="";
        if(KiteListUtils.isNotEmpty(dcPublic)&& dcPublic.size()>0){
            qzOrderDetailUrl=dcPublic.get(0).getCodea();
        }

        return ErrorConsts.SUCCESS.getResult(qzOrderDetailUrl);
    }
}
